package cn.hiapi.user.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicController;
import cn.hiapi.basic.jpa.pageable.PageRequest;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.entity.shop.ShopScore;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shop.vo.OrderReplayJsonVo;
import cn.hiapi.shop.vo.ShopBase;
import cn.hiapi.shopkeeper.entity.order.CommentReplay;
import cn.hiapi.shopkeeper.service.order.CommentReplayService;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2023/1/31 0:47
 */
@RestController("shopkeeperApiCommentReplyController")
@RequestMapping("/user/comment-reply")
public class CommentReplyController extends BasicController {

    private final DispatchContext dispatchContext;

    public CommentReplyController(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }

    @ApiOperation(value = "未读回复数量", notes = "未读回复数量", nickname = "AdinZ")
    @GetMapping("/count")
    public ResponseEntity totalRead() {
        long count = this.dispatchContext.getServiceOne(CommentReplayService.class).count(new QueryWrapper().eq("uid", TokenGet.get().getFid()).eq("read", 0));
        JSONObject data = JsonUtils.builder().put("count", count).build();
        return toSuccess(data);
    }


    @GetMapping("")
    public ResponseEntity<List> page(@RequestParam("page") int page) {
        Token<Object> token = TokenGet.get();
        ResponseEntity<List<CommentReplay>> response = this.dispatchContext.getServiceOne(CommentReplayService.class).findPage(new QueryWrapper().eq("uid", token.getFid()), PageRequest.of(page, 10, Sort.Direction.DESC, "id"));

        List<OrderReplayJsonVo> collect = response.getData().stream().map(e -> {
            if (StringUtils.isEmpty(e.getDataJson())) {
                return null;
            }
            OrderReplayJsonVo vo = JSON.parseObject(e.getDataJson(), OrderReplayJsonVo.class);
            Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(vo.getShopId());
            if (shop != null)
                vo.setShop(shop.copy(new ShopBase()));

            vo.setRead(e.getRead());
            return vo;
        }).filter(e -> e != null).collect(Collectors.toList());
        this.dispatchContext.getServiceOne(CommentReplayService.class).readAll(token.getFid());
        return toSuccess(collect, response.getTotal(), response.getTotalPage());
    }


}
